import { ApiCall } from "tsrpc";
import { ReqOpen_story, ResOpen_story } from "../../../../shared/master/bag/function/PtlOpen_story";
import app from "../../../../app";
import { player, wallet_key } from "../../../../model/fight/body/player";
import { template } from "../../../../shared/master/MsgAction";
import { logger } from "../../../../model/server/logger";

export default async function (call: ApiCall<ReqOpen_story, ResOpen_story>) {
    call.succ({})
    if (call.req.num <= 0) {
        call.req.num = 1;
    }
    let user = call.req._player as player;
    const cfg = app.getCfgData(call.req.id)
    if (!cfg) {
        logger.error('ReqOpen_story')
        return;
    }

    let res = user.check_wallet(0, 0, [{ id: cfg.id, v: call.req.num }], false, '使用支线剧情', true);
    if (!res.state) {
        return;
    }
    const num = app.random(1 * cfg.value * call.req.num, cfg.value * 2 * call.req.num);
    user.wallet_change(wallet_key.gold, num);
    user.sendMsg(template.default, `您打开了${cfg.name}X${call.req.num}：+💰${num}金币！`);
}